package com.example.demo.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.example.demo.po.Movie;
import com.example.demo.service.MovieService;

@Controller
@RequestMapping("/admin")
public class AdminMovieController {
	
	@Autowired
	private MovieService movieService;
	
	@RequestMapping(value="/adminMovie.action",method=RequestMethod.GET)
	public String index(Model model){
		
		List<Movie> movieList = movieService.selectAll();
		
		model.addAttribute("movieList", movieList);
		
		return "admin/movie/movieList";
		
	}
	
	@RequestMapping(value="/toEditMovie.action",method=RequestMethod.GET)
	public String toEdit(Model model,Integer id){
		
		if(id!=null){
			
			Movie movie=movieService.selectById(id);
			
			model.addAttribute("movie", movie);
		}
				
		return "admin/movie/edit";
		
	}
	
	@RequestMapping(value="/editMovie.action",method=RequestMethod.POST)
	public String edit(Movie movie){
		
		if(movie.getId()!=null){
			movieService.update(movie);
		}else{
			movieService.insert(movie);
		}
		
		return "redirect:adminMovie.action";
	}
	
	@RequestMapping(value="/deleteMovie.action",method=RequestMethod.GET)
	public String delete(Integer id){
		
		movieService.delete(id);;
		return "redirect:adminMovie.action";
	}
}
